package com.ucode.springboot.starter.ESsearch.repository.impl;

import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ucode.springboot.starter.ESsearch.config.RestHighLevelClientHelper;
import com.ucode.springboot.starter.ESsearch.repository.BaseElasticsearchTemplate;
import com.ucode.springboot.starter.ESsearch.repository.ElasticsearchIndexTemplate;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;

@Component
public class ElasticsearchIndexTemplateImpl<T> extends BaseElasticsearchTemplate implements ElasticsearchIndexTemplate<T> {

    private static Log log = LogFactory.get(ElasticsearchIndexTemplateImpl.class);
    
    @Autowired
    @Override
    public void setRestHighLevelClientHelper(RestHighLevelClientHelper restHighLevelClientHelper) {
        super.setRestHighLevelClientHelper(restHighLevelClientHelper);
    }

    @Override
    public void createIndex(Class<T> clazz) throws Exception {
        CreateIndexRequest request = buildCreateIndexRequest(clazz);
        CreateIndexResponse createIndexResponse = super.restHighLevelClient.indices().create(request, COMMON_OPTIONS);
        //返回的CreateIndexResponse允许检索有关执行的操作的信息，如下所示：
        boolean acknowledged = createIndexResponse.isAcknowledged();//指示是否所有节点都已确认请求
        System.err.println(acknowledged);
    }
    
    @Override
    public void dropIndex(Class<T> clazz) throws Exception {
        DeleteIndexRequest request = buildDeleteIndexRequest(clazz);
        super.restHighLevelClient.indices().delete(request, COMMON_OPTIONS);
    }

    @Override
    public boolean exists(Class<T> clazz) throws Exception {
        GetIndexRequest request = buildGetIndexRequest(clazz);
        boolean exists = super.restHighLevelClient.indices().exists(request, COMMON_OPTIONS);
        return exists;
    }
    
}
